/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : DisplayFormatter.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 1, 2008       Raghu Sarraff			       Created
 */
package lwf.handler.helper;

import java.util.Collections;
import java.util.List;

import net.lwf.common.util.SortingUtil;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.util.ImageHtml;
import net.lwf.handler.util.WordUtil;

import lwf.handler.entities.RadioData;

/**
 * Type the class description here
 *  
 */
public class DisplayFormatter {
	
	FormatAttribute format = null;
	String formattedQuestion = null;
	
	public DisplayFormatter(FormatAttribute format) {
	    this.format = format;	
	}
	
	public String getFormattedQuestion() {
		if(format.getQuestionType() == 1) {
			setTypeOne();
		}
		else if(format.getQuestionType() == 2) {
			setTypeTwo();
		}
		else if(format.getQuestionType() == 3) {
			setTypeThree();
		}
		else if(format.getQuestionType() == 4) {
			setTypeFour();
		}
		return formattedQuestion;
	}
	
	/**
	 * This format holds the 
	 * 1. Question number
	 * 2. Question information. May contain OK button (upto the process handler).
	 * 3. Display the NEXT button.
	 * 
	 *  |--------------------------------------------------------|-------------|--------|
	 *  | Question #: xx                                                                |
	 *  |--------------------------------------------------------|-------------|--------|
	 *  |                                                        |             |        |
	 *  |                 Question Information                   |             |        |
	 *  |                                                        | NEXT button |        |
	 *  |--------------------------------------------------------|-------------|--------|
	 */
	private void setTypeOne() {
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table align=\"center\" width=\"100%\" border=\"0\" >");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"3\" class=\"leftlabel\" >");
					sbQBlock.append("Question #: " + format.getQuestionNumber());
		        sbQBlock.append("</td>");
		    sbQBlock.append("</tr>");
		    sbQBlock.append("<tr>");
		    	sbQBlock.append("<td width=\"75%\" align=\"center\" nowrap>");
		    		sbQBlock.append(format.getQuestionTable());
		        sbQBlock.append("</td>");
		        sbQBlock.append("<td valign=\"bottom\" align=\"left\" width=\"15%\">");
		        	//sbQBlock.append("<img src=\"image/button/next.gif\" onClick=\"processURL()\" />");
		        	sbQBlock.append(ImageHtml.getButton(IHandlerConstant.NEXT, "processURL()"));
		        sbQBlock.append("</td>");
		        sbQBlock.append("<td width=\"10%\"> &nbsp; </td>");
			sbQBlock.append("</tr>");
	    sbQBlock.append("</table>");		
	    this.formattedQuestion = sbQBlock.toString();
	}
	
	/**
	 * This format holds the 
	 * 1. Question number
	 * 2. Question information 
	 * 3. Display the NEXT button.
	 * 4. Answer box 
	 *    The answer box can be a text box or a text area.
	 *    (attributes can be changed in the handler)
	 * 5. OK button next to answer box to confirm the answer
	 * 
	 *  |--------------------------------------------------------|-------------|--------|
	 *  | Question #: xx                                                                |
	 *  |--------------------------------------------------------|-------------|--------|
	 *  |                                                        |             |        |
	 *  |                 Question Information                   |             |        |
	 *  |                                                        | NEXT button |        |
	 *  |--------------------------------------------------------|-------------|--------|
	 *  |                                                        |                      |
	 *  | 				 Answer box, OK button                   |                      |
	 *  |                                                        |                      |
	 *  |--------------------------------------------------------|-------------|--------|  
	 */
	private void setTypeTwo() {
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table align=\"center\" width=\"100%\" border=\"0\" >");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"3\" class=\"leftlabel\" >");
					sbQBlock.append("Question #: " + format.getQuestionNumber());
		        sbQBlock.append("</td>");
		    sbQBlock.append("</tr>");
		    sbQBlock.append("<tr>");
		    	sbQBlock.append("<td width=\"75%\" align=\"center\" nowrap>");
		    		sbQBlock.append(format.getQuestionTable());
		        sbQBlock.append("</td>");
		        sbQBlock.append("<td valign=\"bottom\" align=\"left\" width=\"15%\">");
		        	//sbQBlock.append("<img src=\"image/button/next.gif\" onClick=\"processURL()\" />");
		        	sbQBlock.append(ImageHtml.getButton(IHandlerConstant.NEXT, "processURL()"));
		        sbQBlock.append("</td>");
		        sbQBlock.append("<td width=\"10%\"> &nbsp; </td>");
			sbQBlock.append("</tr>");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"3\" > &nbsp;</td>");
			sbQBlock.append("</tr>");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td width=\"75%\" align=\"center\" nowrap>");
//					sbQBlock.append("<br><b>Enter answer below and click green button next</b><br>");
//					if(format.isShowtextarea()) {
//						sbQBlock.append("<textarea class=\"textareabox\" name=\"useranswer\" rows=\"2\" cols=\"50\" onChange=\"checkAnswer()\"></textarea>");
//					}
//					else {
//						sbQBlock.append("<input type=\"text\" class=\"answerbox\" name=\"useranswer\" onChange=\"checkAnswer()\"/>");	
//					}
//					sbQBlock.append("&nbsp;<image src=\"image/button/ok.jpg\"/>");
				    sbQBlock.append(this.getAnswerBox());
		        sbQBlock.append("</td>");
		        sbQBlock.append("<td colspan=\"2\"> &nbsp; </td>");
			sbQBlock.append("</tr>");
		sbQBlock.append("</table>");		
	    this.formattedQuestion = sbQBlock.toString();
	}
	
	private String getAnswerBox() {
		StringBuffer sbAnswer = new StringBuffer("<table align=\"center\" border=\"0\" >");
			sbAnswer.append("<tr>");
				sbAnswer.append("<td colspan=\"7\" class=\"tablesubheader\" >");
					sbAnswer.append("Enter answer below and click green button next");
				sbAnswer.append("</td>");	
			sbAnswer.append("</tr>");
			sbAnswer.append("<tr>");
				sbAnswer.append("<td colspan=\"7\"> &nbsp; </td>");	
			sbAnswer.append("</tr>");			
			sbAnswer.append("<tr>");
				if(format.getAnswerType() != null && format.getAnswerType().equalsIgnoreCase("time")) {
					sbAnswer.append("<td align=\"right\" >");
						//sbAnswer.append("<input type=\"text\" style=\"width:50px\" class=\"answerbox\" name=\"hour\" />");
						sbAnswer.append("<b>hour</b><br><input type=\"text\" style=\"" + format.getTextboxwidth() + "\" class=\"answerbox\" name=\"hour\" />");
					sbAnswer.append("</td>");
					sbAnswer.append("<td class=\"datacenter\">");
						sbAnswer.append("&nbsp;:&nbsp;");
					sbAnswer.append("</td>");
					sbAnswer.append("<td>");
						//sbAnswer.append("<input type=\"text\" style=\"width:50px\" class=\"answerbox\" name=\"minute\" />");
						sbAnswer.append("<b>minute</b><br><input type=\"text\" style=\"" + format.getTextboxwidth() + "\" class=\"answerbox\" name=\"minute\" />");
					sbAnswer.append("</td>");	
					sbAnswer.append("<td>");
						sbAnswer.append("&nbsp;");
					sbAnswer.append("</td>");
					if(format.showAmpmBox()) {
						sbAnswer.append("<td>");
						sbAnswer.append("<select name=\"ampmkey\" class=\"dropdownbox\" >");
							sbAnswer.append("<option value=\"\" > -- Select a value -- </option>");
							sbAnswer.append("<option value=\"AM\" > AM </option>");
							sbAnswer.append("<option value=\"PM\" > PM </option>");
						sbAnswer.append("</select>");
						sbAnswer.append("</td>");
						sbAnswer.append("<td>");
							sbAnswer.append("&nbsp;");
						sbAnswer.append("</td>");
					    sbAnswer.append("<td>");
					    	sbAnswer.append("<image src=\"image/button/ok.jpg\" onclick=\"checkAnswer()\" />");
				        sbAnswer.append("</td>");
					}
					else {
					    sbAnswer.append("<td colspan=\"3\">");
				    		sbAnswer.append("<image src=\"image/button/ok.jpg\" onclick=\"checkAnswer()\" />");
				    	sbAnswer.append("</td>");						
					}
				}
				else {
					sbAnswer.append("<td colspan=\"5\" >");
						if(format.isShowtextarea()) {
							//sbAnswer.append("<textarea class=\"textareabox\" name=\"useranswer\" rows=\"2\" cols=\"50\" onChange=\"checkAnswer()\"></textarea>");
							sbAnswer.append("<textarea class=\"textareabox\" name=\"useranswer\" " +
									          "rows=\" " + format.getTextarearows() + "\" " +
									          "cols=\"" + format.getTextareacolumns() + "\" " +
									          "onChange=\"checkAnswer()\"></textarea>");
						}
						else {
							if(format.getTextboxwidth() == null) {
								sbAnswer.append("<input type=\"text\" class=\"answerbox\" name=\"useranswer\" onChange=\"checkAnswer()\"/>");	
							}
							else {
								sbAnswer.append("<input type=\"text\" style=\"" + format.getTextboxwidth() + "\" class=\"answerbox\" name=\"useranswer\" onChange=\"checkAnswer()\"/>");
							}
								
						}
					sbAnswer.append("</td>");
					sbAnswer.append("<td>");
						sbAnswer.append("&nbsp;");
					sbAnswer.append("</td>");
				    sbAnswer.append("<td>");
				    	sbAnswer.append("&nbsp;<image src=\"image/button/ok.jpg\"/>");
			        sbAnswer.append("</td>");
				}
			sbAnswer.append("</tr>");
		sbAnswer.append("</table>");
		return sbAnswer.toString();
	}
	
	/**
	 * This format holds the 
	 * 1. Question number
	 * 2. Question information. 
	 * 3. Radio buttons to choose answers
	 * 4. Display the NEXT button.
	 * 
	 *  |--------------------------------------------------------|-------------|--------|
	 *  | Question #: xx                                                                |
	 *  |--------------------------------------------------------|-------------|--------|
	 *  |                                                        |             |        |
	 *  |                 Question Information                   |             |        |
	 *  |                                                        |             |        |
	 *  | |--Choose your answer ------------------|				 |			   |		|
	 *  | |    option1  option2  option3  option4 |				 |			   |		|
	 *  | |---------------------------------------|				 | NEXT button |		|
	 *  |--------------------------------------------------------|-------------|--------|
	 */
	private void setTypeThree() {
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table align=\"center\" width=\"100%\" border=\"0\" >");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"3\" class=\"leftlabel\" >");
					sbQBlock.append("Question #: " + format.getQuestionNumber());
		        sbQBlock.append("</td>");
		    sbQBlock.append("</tr>");
		    sbQBlock.append("<tr>");
		    	sbQBlock.append("<td width=\"75%\" align=\"center\" nowrap>");
		    		sbQBlock.append(format.getQuestionTable());
		        sbQBlock.append("</td>");
		        sbQBlock.append("<td valign=\"bottom\" align=\"left\" width=\"15%\">");
		        	sbQBlock.append(ImageHtml.getButton(IHandlerConstant.NEXT, "processURL()"));
		        sbQBlock.append("</td>");
		        sbQBlock.append("<td width=\"10%\"> &nbsp; </td>");
			sbQBlock.append("</tr>");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"3\"> &nbsp; </td>");
	        sbQBlock.append("</tr>");
			sbQBlock.append("<tr>");
			sbQBlock.append("<td width=\"75%\" class=\"chooseanswer\">");
				sbQBlock.append("Choose your answer from below");
			    sbQBlock.append("</td>");
			    sbQBlock.append("<td colspan=\"2\"> &nbsp; </td>");
			sbQBlock.append("</tr>");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td width=\"75%\" align=\"center\" valign=\"top\" nowrap>");
				    sbQBlock.append(generateRadioChoices(format.getRadioAnswers()));
				sbQBlock.append("</td>");
				sbQBlock.append("<td colspan=\"2\"> &nbsp; </td>");
			sbQBlock.append("</tr>");
	    sbQBlock.append("</table>");		
	    this.formattedQuestion = sbQBlock.toString();
	}
	
	/**
	 * This format holds the 
	 * 1. Question number
	 * 2. Question information 
	 * 3. OK button next to Question information
	 * 3. Display the NEXT button.
	 *  |--------------------------------------------------------|-------------|--------|
	 *  | Question #: xx                                                                |
	 *  |--------------------------------------------------------|-------------|--------|
	 *  |                                                        |             |        |
	 *  |                 Question Information                   |             |        |
	 *  |     Click the green button to see the answer OK        | NEXT button |        |
	 *  |--------------------------------------------------------|-------------|--------|
	 */
	private void setTypeFour() {
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table align=\"center\" width=\"100%\" border=\"0\" >");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"3\" class=\"leftlabel\" >");
					sbQBlock.append("Question #: " + format.getQuestionNumber());
		        sbQBlock.append("</td>");
		    sbQBlock.append("</tr>");
		    sbQBlock.append("<tr>");
		    	sbQBlock.append("<td width=\"75%\" align=\"center\" nowrap>");
		    		sbQBlock.append(format.getQuestionTable());
		        sbQBlock.append("</td>");
		        sbQBlock.append("<td valign=\"bottom\" align=\"left\" width=\"15%\">");
		        	sbQBlock.append(ImageHtml.getButton(IHandlerConstant.NEXT, "processURL()"));
		        sbQBlock.append("</td>");
		        sbQBlock.append("<td width=\"10%\"> &nbsp; </td>");
			sbQBlock.append("</tr>");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"3\" class=\"infodatacenter\"> Click the green check button to see the answer.");
				    
		    		if(format.isAddOnClickForOK()) {
		    			sbQBlock.append("&nbsp;&nbsp;<img src=\"image/button/ok.jpg\" onclick=\"checkAnswer()\" />");
		    		}
		    		else {
		    			sbQBlock.append("&nbsp;&nbsp;<img src=\"image/button/ok.jpg\" />");	
		    		}
		    	sbQBlock.append("</td>");
	        sbQBlock.append("</tr>");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"3\"> &nbsp; </td>");
			sbQBlock.append("</tr>");
	    sbQBlock.append("</table>");		
	    this.formattedQuestion = sbQBlock.toString();
	}
	
	private String generateRadioChoices(List lstData) {
		
		if(hasRadioData(lstData)) {
			return processRadioData(lstData);
		}
		List newdata = WordUtil.removeDuplicates(lstData);
		if(newdata != null) {
			Collections.shuffle(newdata);
			this.processAnswers(newdata);
			int size = newdata.size();
			
			String width = String.valueOf(size * (2+5+5)) + "%";
			
			StringBuffer sbradio = new StringBuffer("<table width=\"" + width + "\" border=\"0\" class=\"radiotable\" >");
				sbradio.append("<tr>");
				for(int i=0; i<newdata.size(); i++) {
					sbradio.append("<td width=\"2%\">");
					String data = (String) newdata.get(i);
					sbradio.append("<input type=\"radio\" name=\"choice\" value=\" ");
					sbradio.append(data);
					sbradio.append("\" onclick=\"checkAnswer()\"  />");
		    	    sbradio.append("</td>");
		    	    sbradio.append("<td width=\"5%\" class=\"leftlabel\" >");
		    	    	sbradio.append(data);
		    	    sbradio.append("</td>");
		    	    sbradio.append("<td width=\"5%\"> &nbsp; </td>");
				}
				sbradio.append("</tr>");
			sbradio.append("</table>");
			return sbradio.toString();			
		}
		return "";
	}
	
	private boolean hasRadioData(List lstData) {
		Object firstelement = lstData.get(0);
		if(firstelement instanceof RadioData) {
			return true;
		}
		return false;
	}
	
	private String processRadioData(List lstData) {
        if(lstData != null && lstData.size() != 0) { 		
        	StringBuffer sbradio = null;    
    		if(!format.isVerticalradioanswers()) {
				int size = lstData.size();
				String width = String.valueOf(size * (2+5+5)) + "%";
				sbradio = new StringBuffer("<table width=\"" + width + "\" border=\"0\" class=\"radiotable\" >");
		    	sbradio.append("<tr>");
				for(int i=0; i<lstData.size(); i++) {
					RadioData data = (RadioData) lstData.get(i);
					sbradio.append("<td width=\"2%\">");
						sbradio.append("<input type=\"radio\" name=\"choice\" value=\" ");
						sbradio.append(data.getAnswer());
						sbradio.append("\" onclick=\"checkAnswer()\"  />");
		    	    sbradio.append("</td>");
		    	    sbradio.append("<td width=\"5%\" class=\"leftlabel\" >");
		    	    	sbradio.append(data.getDisplay());
		    	    sbradio.append("</td>");
		    	    sbradio.append("<td width=\"5%\"> &nbsp; </td>");
				}
				sbradio.append("</tr>");
		    }
		    else {
		    	sbradio = new StringBuffer("<table width=\"100%\" border=\"0\" class=\"radiotable\" >");
		    	lstData = SortingUtil.shuffle(lstData);
		    	for(int i=0; i<lstData.size(); i++) {
		    		RadioData data = (RadioData) lstData.get(i);
		    		sbradio.append("<tr>");
						sbradio.append("<td width=\"3%\">");
							sbradio.append("<input type=\"radio\" name=\"choice\" value=\" ");
							sbradio.append(data.getAnswer());
							sbradio.append("\" onclick=\"checkAnswer()\"  />");
		    	        sbradio.append("</td>");
		    	        sbradio.append("<td width=\"5%\"> &nbsp; </td>");
		    	        sbradio.append("<td width=\"" + (100 - 8) + "%\" class=\"leftlabel\" >");
		    	    		sbradio.append(data.getDisplay());
		    	        sbradio.append("</td>");
		    		sbradio.append("</tr>");
		    	}
		    }
			sbradio.append("</table>");
			return sbradio.toString();			
        }

		return "";
	}	
	
	
	private void processAnswers(List answers) {
		boolean removed = false;
		if(answers != null) {
			for(int i=0; i<answers.size(); i++) {
				String choice = (String) answers.get(i);
				if(choice.equalsIgnoreCase("none")) {
					answers.remove(i);
					removed = true;
				}
			}
			if(removed) {
				answers.add("None");
			}
		}
	}
	/*
	private String generateQuestionTable(String qTitle, String question) {
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table border=\"0\"  width=\"55%\" align=\"center\" cellspacing=\"2\" cellpadding=\"0\">");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td class=\"tableHeader\" colspan=\"3\" nowrap>");
					sbQBlock.append(qTitle);
				sbQBlock.append("</td>");
			sbQBlock.append("</tr>");			
			sbQBlock.append("<tr>");
				sbQBlock.append("<td width=\"30%\"> &nbsp; </td>");
       			sbQBlock.append("<td width=\"5%\"> &nbsp; </td>");
       			sbQBlock.append("<td width=\"15%\"> &nbsp; </td>");
		    sbQBlock.append("</tr>");
		    sbQBlock.append("<tr>");
	   			sbQBlock.append("<td colspan=\"3\" class=\"datacenter\">");
	   				sbQBlock.append(question);
	   			sbQBlock.append("</td>");
		    sbQBlock.append("</tr>");
        	sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"3\" > &nbsp; </td>"); 
			sbQBlock.append("</tr>");
        	sbQBlock.append("<tr>");
				sbQBlock.append("<td class=\"tableHeader\" colspan=\"3\" nowrap>");
					sbQBlock.append("<hr>");
				sbQBlock.append("</td>");
        	sbQBlock.append("</tr>");
        sbQBlock.append("</table>");
        
        String abcd = sbQBlock.toString();
		return sbQBlock.toString();	
	}*/
	
}

